package com.agu.mgt.sys.controller;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;

import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;
import com.agu.mgt.sys.controller.AbstractController;
import com.agu.entity.sys.SysLogEntity;
import com.agu.mgt.sys.service.SysLogService;
import com.agu.utils.Query;
import com.github.pagehelper.Page;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 系统日志表
 * 
 * @author 
 * @email 
 * @date 2018-09-01 11:36:08
 */
 @SuppressWarnings("rawtypes")
@RestController
@Api("系统日志表")
@RequestMapping("syslog")
public class SysLogController extends AbstractController{
	@Autowired
	private SysLogService sysLogService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("syslog:list")
	public Result list(@RequestBody Map params){

		// 查询列表数据
		Query query = new Query(params);
		Page<SysLogEntity> page = sysLogService.queryPageList(query);
		return ResultGenerator.successPage(page);
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/info/{id}")
	@ApiOperation(value = "信息", notes = "说明")
	@RequiresPermissions("syslog:list")
	public Result info(@PathVariable("id") Long id){
		SysLogEntity sysLog = sysLogService.queryObject(id);
		
		return ResultGenerator.successData(sysLog);
	}
	
	/**
	 * 保存
	 */
	@PostMapping("/save")
	@ApiOperation(value = "保存", notes = "说明")
	@RequiresPermissions("syslog:save")
	public Result save(@RequestBody SysLogEntity sysLog){
		sysLogService.save(sysLog);
		
		return ResultGenerator.success("保存成功");
	}
	
	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("syslog:update")
	public Result update(@RequestBody SysLogEntity sysLog){
		sysLogService.update(sysLog);
		
		return ResultGenerator.success("修改成功");
	}
	
	/**
	 * 删除
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("syslog:list")
	public Result delete(@RequestBody Long[] ids){
		sysLogService.deleteBatch(ids);
		
		return ResultGenerator.success("删除成功");
	}
	
	@RequestMapping("/download")
	@ApiOperation(value = "下载", notes = "说明")
	@RequiresPermissions("syslog:list")
	public void download(HttpServletRequest request, HttpServletResponse response) {

		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		
		String query = orgRequest.getParameter("query");

		HashMap querymap = JSON.parseObject(query, HashMap.class);

		List<SysLogEntity> list = sysLogService.queryList(querymap);

		if (list != null && list.size() > 0) {
			Map map = new HashMap();

               Map<Integer, String> stautsMap = new HashMap<Integer, String>();

                stautsMap.put(0, "成功");
                stautsMap.put(1, "失败");
            map.put("stautsMap", stautsMap);

			List<Column> clist = new ArrayList();
			Column cloumn = null;

            cloumn = new Column("", "id", 1);
			clist.add(cloumn);
            cloumn = new Column("用户名", "username", 1);
			clist.add(cloumn);
            cloumn = new Column("用户操作", "operation", 1);
			clist.add(cloumn);
            cloumn = new Column("请求方法", "method", 1);
			clist.add(cloumn);
            cloumn = new Column("请求参数", "params", 1);
			clist.add(cloumn);
            cloumn = new Column("IP地址", "ip", 1);
			clist.add(cloumn);
            cloumn = new Column("状态", "stauts", 2);
			clist.add(cloumn);
            cloumn = new Column("备注", "remark", 1);
			clist.add(cloumn);
            cloumn = new Column("消息", "message", 1);
			clist.add(cloumn);
            cloumn = new Column("时间", "createDate", 1);
			clist.add(cloumn);
			try {
				ExcelExport export = exportList(list, clist, map);
				if (export != null) {
					this.downExcel(export, response);
					//this.downzip(export, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
